//
//  PPCouponViewController.m
//  PPDL
//
//  Created by 王洋 on 2019/11/11.
//  Copyright © 2019 王洋. All rights reserved.
//

#import "PPCouponCell.h"

#import "PPCouponViewController.h"

@interface PPCouponViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableview;

@property (weak, nonatomic) IBOutlet UISegmentedControl *segmengControl;

@property (nonatomic, strong) NSMutableArray <PPCouponModel *>* dataArray;

@property (nonatomic, assign) CGFloat rowHeight;

@property (nonatomic, copy) NSString *status;

@end

@implementation PPCouponViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.status = @"1";
    [self request_couponList];
    [self initView];
}

- (void)request_couponList {
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:self.status forKey:@"status"];
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,TT_MEMBER_RECOMMEND_COUPON];
    
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {

        [[TTNoResultManager shareManager] hideNoResultWithView:self.view];
        [[TTNoResultManager shareManager] hideNoResultWithView:self.tableview];

        //返回data为字典
        if ([data isKindOfClass:[NSDictionary class]]) {
            NSString * code = [data valueForKey:@"code"];
            //请求成功
            if ([code isEqualToString:@"200"]) {
                
                NSDictionary *resultDic = [data valueForKey:@"data"];
                if ([resultDic isKindOfClass:[NSDictionary class]]) {
                    
                    NSArray *recommendRecordRes = [resultDic valueForKey:@"recommendRecordRes"];

                    if ([recommendRecordRes isKindOfClass:[NSArray class]]) {

                        if (recommendRecordRes.count == 0) {
                            [[TTNoResultManager shareManager] showNoResultWithStateInView:self.tableview errorCode:kTTViewStateNoData btnTitle:@"刷新" didPressButton:^{
                                
                                [self request_couponList];
                            }];
                        }
                        
                        for (NSDictionary *dic in recommendRecordRes) {
                            
                            PPCouponModel *model = [[PPCouponModel alloc] init];
                            model.itemMemo = [dic valueForKey:@"message"];
                            model.couponType = (PPCouponType)[[dic valueForKey:@"status"] integerValue];
                            
                            [self.dataArray addObject:model];
                        }
                    }
                }
                
            } else {
                
                //检查到未登录
                if ([code isEqualToString:@"401"]) {
                    [[TTUserModel sharedObject] initialization];
                    [self openLoginWithCompletion:^(BOOL isLogin, NSDictionary * _Nonnull options) {
                        
                        if (isLogin) {
                            [self request_couponList];
                        } else {
                            [self.navigationController popToRootViewControllerAnimated:NO];
                        }
                    }];
                    
                } else {

                    [[TTNoResultManager shareManager] showNoResultWithStateInView:self.tableview errorCode:kTTViewStateNoData btnTitle:@"刷新" didPressButton:^{
                        
                        [self request_couponList];
                    }];
                    
                }
                [SVProgressHUD showErrorWithStatus:[data valueForKey:@"msg"]];
                [SVProgressHUD dismissWithDelay:1];
            }
            
            //刷新
        } else {
            
            //返回为空
            [SVProgressHUD showErrorWithStatus:@"返回为空"];
            [SVProgressHUD dismissWithDelay:1];
        }
        
        [self.tableview reloadData];
        
    } FailedBlock:^(NSError * _Nonnull error) {
        
        WEAKSELF
        [[TTNoResultManager shareManager] showNoResultWithStateInView:self.view errorCode:error.code btnTitle:@"重新加载" didPressButton:^{
            [weakSelf request_couponList];
        }];
    }];
}

- (void)initView {
    
    self.title = @"一拍必得券";
    
    self.rowHeight = TTScreenWidth * 410 / 1083;

    //设置选中字体颜色
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:UIColorHex(0xffffff),NSForegroundColorAttributeName,nil];
    [self.segmengControl setTitleTextAttributes:dic forState:UIControlStateSelected];
    
    self.tableview.backgroundColor = PP_Default_Color_Gray_F1F1F1;
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableview.showsHorizontalScrollIndicator = NO;
    self.tableview.showsVerticalScrollIndicator = NO;
    
    self.tableview.estimatedSectionHeaderHeight = 0;
    self.tableview.estimatedSectionFooterHeight = 0;
    self.tableview.estimatedRowHeight = 0;
    
    [self.tableview registerNib:[UINib nibWithNibName:@"PPCouponCell" bundle:nil] forCellReuseIdentifier:@"PPCouponCellID"];

}


#pragma mark tableview delegate & datasource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    PPCouponCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PPCouponCellID" forIndexPath:indexPath];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;

    if (self.dataArray.count > indexPath.row) {
        [cell fillCellWithModel:self.dataArray[indexPath.row]];
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return self.rowHeight;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataArray.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if ([self.status isEqualToString:@"1"]) {
        [self.navigationController changeNormalNavigation];
        [self.navigationController popToRootViewControllerAnimated:NO];
        [PPTabBarController setCurrentIndex:PPTabbarIndexTypeShop];
    }
}

- (IBAction)changeUseStatus:(id)sender {
    
    if (self.segmengControl.selectedSegmentIndex == 0) {
        self.status = @"1";
        
    } else if (self.segmengControl.selectedSegmentIndex == 1) {
        self.status = @"0";

    }
    
    [self.dataArray removeAllObjects];
    [self request_couponList];
}

- (NSMutableArray<PPCouponModel *> *)dataArray {
    
    if (!_dataArray) {
        _dataArray = @[].mutableCopy;
    }
    return _dataArray;
}

@end
